(function(a){
	jQuery.fn.minipicker = function(m){
		var e = {
			showOn		: "click",
			hideOn		: "click",
			weekOn		: false,
			timeOn		: false,
			left		: 0,
			top			: 30,
			split		: "/",
			beginYear	: 1979,
			endYear		: 2050,
			keepLive	: false
		};
		var m = $.extend(e, m), h, j, c, f = this
				,b = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
				,k = "<div class='mini-dpicker'>"
						+ "<div class='mini-dpicker-header'>"
							+ "<a class='mini-dpicker-premonth' title='上个月'style='float:left;'><<</a>"
							+ "<select class='mini-year'></select>"
							+ "<select class='mini-month'></select>"
							+ "<a class='mini-dpicker-nextmonth' title='下个月'style='float:right;'>>></a>"
						+ "</div>"
						+ "<table cellpadding='1'>"
							+ "<tr><th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr>" 
						+ "</table>" 
					+ "</div>";
		if(!m.keepLive){
			this.live(m.showOn, function(){
						if($(this).attr("mini-flag") == null){
							var n = g(this).appendTo($("body")).fadeIn();
							$(this).attr("mini-flag", true);
							n.delegate(".mini-dpicker-hover", m.hideOn,
									function(){
										d(this, f);
									}
							);
						}
					});
		}else{
			g(this).appendTo(this).attr("keepLive", true).css("position", "relative").show();
		}
		function g(o){

			var f = $(o).offset();
			var s = $(k).css({
						position	: "absolute",
						zIndex		: 100,
						display		: "none"
					}), p = s.find(".mini-year"), q = s.find(".mini-month"), n = {
				top		: f.top + m.top + "px",
				left	: f.left + $(o).width() + m.left + "px"
			};
			var v = $.trim($(o).val());
			if(v == '' || v == '0000-00-00'){
				var t = new Date();
			}else{
				var r = v.split('-');
				r[1] = parseInt(r[1], 10) - 1;
				r[2] = parseInt(r[2], 10);
				var t = new Date(r[0], r[1], r[2]);
			}
			h = t.getDate();
			j = t.getFullYear();
			c = t.getMonth() + 1;
			s.css({
				top		: n.top,
				left	: n.left
			});
			for(var r = m.beginYear; r <= m.endYear; r++){
				$("<option value='" + r + "'>" + r + "年</option>").appendTo(p);
			}
			for(var r = 1; r <= 12; r++){
				$("<option value='" + r + "'>" + r + "月</option>").appendTo(q);
			}
			l(j, c, s);
			return s
		}
		$(".mini-dpicker-nextmonth").live("click", function(){
			var o = $(this), r = o.parent().next("table"), n = r.attr("year"), q = r.attr("month");
			q++;
			if(q > 12){
				q = 1;
				n++;
			}
			var p = o.parents(".mini-dpicker");
			l(n, q, p);
		}
		);
		$(".mini-dpicker-premonth").live("click", function(){
			var p = $(this), q = p.parent().next("table"), n = q.attr("year"), o = q
					.attr("month");
			o--;
			if(o < 1){
				o = 12;
				n--;
			}
			l(n, o, p.parents(".mini-dpicker"));
		}
		);
		$(".mini-year").live("change", function(){
					var n = $(this), o = n.val(), p = n.next().val();
					l(o, p, n.parents(".mini-dpicker"));
				});
		$(".mini-month").live("change", function(){
					var n = $(this), p = n.val(), o = n.prev().val();
					l(o, p, n.parents(".mini-dpicker"));
				});
		function d(u, q){
			var r = $(u), v = r.parents("table"), t = r.html(), n;
			if(r.html() != ""){
				var year = v.attr("year");
				var month = v.attr("month");
				year = year < 10 ? '0' + year : year;
				month = month < 10 ? '0' + month : month;
				t = t < 10 ? '0' + t : t;
				n = year + m.split + month + m.split + t;
				if(m.weekOn){
					n = n + " " + b[r.attr("week")];
				}
				if(m.timeOn){
					var s = new Date(), p = s.getHours(), o = s.getMinutes();
					second = s.getSeconds();
					n = n + " " + p + ":" + o + ":" + second;
				}
				q.val(n);
				q.removeAttr("mini-flag");
				r.parents(".mini-dpicker").fadeOut("fast", function(){
							$(this).remove();
						});
			}else{
				q.val("");
			}
		}
		$(".mini-dpicker td").live("mouseover", function(){
					$(this).addClass("mini-dpicker-hover");
				}).live("mouseout", function(){
					$(this).removeClass("mini-dpicker-hover");
				});
		function l(w, t, s){
			var y = s.find("table"), o = new Date(w, t - 1, 1).getDay(), x = i(
					t, w
			), u = true;
			y.attr("year", w).attr("month", t);
			s.find(".mini-year").val(w);
			s.find(".mini-month").val(t);
			y.find("tr:not(:first)").remove();
			if(j == w && c == t){
				u = true;
			}else{
				u = false;
			}
			var p = 1, v = $("<tr/>"), q = null;
			for(var r = 0; r < 7; r++){
				q = $("<td/>");
				if(r < o){
					v.append(q);
				}else{
					if(p == h && u){
						q.addClass("mini-dpicker-selected").html(p).attr("week", r );
						p++
					}else{
						q.html(p).attr("week", r);
						p++;
					}
				}
				v.append(q);
				if(r == 6){
					y.append(v);
				}
			}
			var r = 0, v = $("<tr/>");
			while(r < 7){
				if(p <= x){
					if(p == h && u){
						v.append($("<td/>").addClass("mini-dpicker-selected") .html(p).attr("week", r));
					}else{
						v.append($("<td/>").html(p).attr("week", r));
					}
				}else{
					v.append($("<td/>"));
				}
				r++;
				p++;
				if(r == 7){
					y.append(v);
					v = $("<tr/>");
					if(p < x){
						r = 0;
					}
				}
			}
		}
		function i(p, n){
			p = p - 1;
			var q = ((n % 4 == 0 && n % 100 != 0) || n % 400 == 0) ? true : false;
			var o;
			switch(parseInt(p)){
				case 0:
				case 2:
				case 4:
				case 6:
				case 7:
				case 9:
				case 11:
					o = 31;
					break;
				case 3:
				case 5:
				case 8:
				case 10:
					o = 30;
					break;
				case 1:
					o = q ? 29 : 28;
			}
			return o;
		}
		return this;
	}
})(jQuery);